package com.kaili.basic.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseNewsIssueAddDto;
import com.kaili.basic.pojo.dto.BaseNewsIssueEditDto;
import com.kaili.basic.pojo.dto.BaseNewsIssueQueryDto;
import com.kaili.basic.pojo.dto.BaseNewsReplyAddDto;
import com.kaili.basic.pojo.entity.BaseNewsIssueVO;
import com.kaili.basic.pojo.entity.BaseNewsReplyVO;
import com.kaili.basic.pojo.entity.GlobalClob;
import com.kaili.basic.pojo.vo.BaseNewsVO;
import com.kaili.basic.service.BaseNewsService;
import com.kaili.basic.service.GlobalClobService;
import com.kaili.basic.service.NewsReplyService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: dxj
 * @descrinption: Created by dxj on 18-6-25.
 * @modified:
 */
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "信息通知", description = "信息通知相关API")
public class NewsController {

    @Autowired
    private BaseNewsService newsService;

    @Autowired
    private NewsReplyService newsReplyService;

    @Autowired
    private GlobalClobService globalClobService;


    //新增新闻接口(通知发布页面)
    @PostMapping(value = "/news")
    @ApiOperation(value = "新增通知", notes = "新增新闻通知")
    public RestStatus saveNews(@Validated @RequestBody BaseNewsIssueAddDto addDto){
        BaseNewsIssueVO newsIssue=new BaseNewsIssueVO();
        BeanUtils.copyProperties(addDto,newsIssue);
        List<String> fileTypeList = new ArrayList<String>();
        List<byte[]> byteList = new ArrayList<byte[]>();

        newsService.addNews(newsIssue,fileTypeList,byteList);
        return CommonStatus.ADD_OK;

    }


    @DeleteMapping(value = "/news/{newsId}")
    @ApiOperation(value = "删除新闻接口", notes = "删除新闻接口")
    public CommonStatus delNews(@ApiParam("通知ID集合")@PathVariable("newsId") Long newsId){
        try{
            BaseNewsIssueVO issue = newsService.getNewsIssueById(newsId);
            issue.setRdStatus(0);
            newsService.ediNews(issue);
        }
        catch (Exception e){
            System.out.println(e+"出现意外异常");
            return CommonStatus.DELETE_FAIL;
        }
        return CommonStatus.DELETE_OK;
    }


    @PutMapping(value = "/news")
    @ApiOperation(value = "修改通知", notes = "修改闻通知")
    public CommonStatus ediNews(@Validated @RequestBody BaseNewsIssueEditDto editDto){
        BaseNewsIssueVO issueVO=new BaseNewsIssueVO();
        BeanUtils.copyProperties(editDto,issueVO);
        issueVO.setRdStatus(1);
        boolean flag = newsService.ediNews(issueVO);
        if (!flag){
            return CommonStatus.UPDATE_FAIL;
        }else{
            return CommonStatus.UPDATE_OK;
        }

    }

    @GetMapping(value = "/news/forList")
    @ApiOperation(value = "查询新闻接口(通知发布页面)", notes = "查询新闻接口(通知发布页面)")
    public Page<BaseNewsVO> queryNewsIssue(@ModelAttribute BaseNewsIssueQueryDto queryDto){
        BaseNewsIssueVO issueVO=new BaseNewsIssueVO();
        BeanUtils.copyProperties(queryDto,issueVO);
        issueVO.setCurSiteId(LoginDetailUtils.getSiteId().longValue());
        Page<BaseNewsVO> result = newsService.queryPagedList(new Page<BaseNewsIssueVO>(issueVO.getPage(),issueVO.getPageSize()),issueVO);
        return result;
    }

    @GetMapping(value = "/news/forNews")
    @ApiOperation(value = "查询新闻接口(新闻查询页面)", notes = "查询新闻接口(新闻查询页面)")
    public Page<BaseNewsVO> queryForNews(@ModelAttribute BaseNewsIssueQueryDto queryDto){
        BaseNewsIssueVO issueVO=new BaseNewsIssueVO();
        BeanUtils.copyProperties(queryDto,issueVO);
        issueVO.setCurSiteId(LoginDetailUtils.getSiteId().longValue());
        Page<BaseNewsVO> result = newsService.queryForNews(new Page<BaseNewsIssueVO>(issueVO.getPage(),issueVO.getPageSize()),issueVO);
        return result;
    }


    //新闻查询标题模糊查询接口
    @RequestMapping(value = "/queryLikeTitle",method = RequestMethod.POST)
    public List<String> queryLikeTitle(@RequestBody String newsTitle){
        if (newsTitle != null && newsTitle != ""){
            List<String> titleValues = newsService.queryLikeTitle(newsTitle);
            return titleValues;
        }else
            return null;
    }


    @GetMapping(value = "/news/clickNews/{newsId}")
    @ApiOperation(value = "通知查询页面双击接口", notes = "通知查询页面双击接口")
    public List<BaseNewsReplyVO> douClickNews(@ApiParam("新闻/消息ID")@PathVariable("newsId") Long newsId){
        BaseNewsIssueVO issue = newsService.getNewsIssueById(newsId);
        //设置通知为已读
        issue.setIsRead(1L);
        issue.setSeeCount(issue.getSeeCount()+1);
        boolean flag = newsService.ediNewsIsRead(issue);
        if (!flag){
            return null;
        }else{
            //根据通知ID查询回复信息
            List<BaseNewsReplyVO> newsReply = newsReplyService.queryReplyByNewsId(issue.getNewsId());
            return newsReply;
        }
    }

    @PostMapping(value = "/news/reply")
    @ApiOperation(value = "新闻查询页面回复接口(新增)", notes = "新闻查询页面回复接口(新增)")
    public RestStatus reply(@RequestBody BaseNewsReplyAddDto addDto){
        BaseNewsReplyVO reply=new BaseNewsReplyVO();
        BeanUtils.copyProperties(addDto,reply);
        if (reply.getNewsId() == null){
            return CommonStatus.NEWS_ID_NULL;
        }
        reply.setSiteId(LoginDetailUtils.getSiteId().longValue());
        BaseNewsIssueVO issue = newsService.getNewsIssueById(reply.getNewsId());
        GlobalClob clob = globalClobService.getClobById(issue.getNewsId());
        issue.setClobValue(clob.getClobValue());
        issue.setReplyCount(issue.getReplyCount()+1);
        if (issue!= null){
            boolean flag = newsService.ediNews(issue);
        }
        newsReplyService.addNewsReply(reply);
        return CommonStatus.ADD_REPLY_SUCCESS;
    }

    @GetMapping(value = "/news/reply/{newsId}")
    @ApiOperation(value = "新闻查询页面回复接口(查询)", notes = "新闻查询页面回复接口(查询)")
    public List<BaseNewsReplyVO> getReplysByNewsId(@ApiParam("新闻/消息ID")@PathVariable("newsId") Long newsId){
        //根据通知ID查询回复信息
        if (newsId != null){
            List<BaseNewsReplyVO> newsReply = newsReplyService.queryReplyByNewsId(newsId);
            return newsReply;
        }else {
            return null;
        }
    }
}



